package cn.kz.hrm.web.controller;

import cn.kz.hrm.service.IUserloginService;
import cn.kz.hrm.domain.Userlogin;
import cn.kz.hrm.query.UserloginQuery;
import cn.kz.hrm.util.AjaxResult;
import cn.kz.hrm.util.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/userlogin")
public class UserloginController {
    @Autowired
    public IUserloginService userloginService;

    /**
     * 保存和修改公用的
     * @param userlogin 传递的实体
     * @return Ajaxresult转换结果
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public AjaxResult save(@RequestBody Userlogin userlogin) {
        try {
            if (userlogin.getId() != null) {
                userloginService.updateById(userlogin);
            } else {
                userloginService.insert(userlogin);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！" + e.getMessage());
        }
    }

    /**
     * 删除对象信息
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            userloginService.deleteById(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！" + e.getMessage());
        }
    }

    //获取用户
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Userlogin get(@PathVariable("id") Long id) {
        return userloginService.selectById(id);
    }


    /**
     * 查看所有的员工信息
     *
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public List<Userlogin> list() {

        return userloginService.selectList(null);
    }


    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @RequestMapping(value = "/pagelist", method = RequestMethod.POST)
    public PageList<Userlogin> json(@RequestBody UserloginQuery query) {
        Page<Userlogin> page = new Page<Userlogin>(query.getPage(), query.getRows());
        page = userloginService.selectPage(page);
        return new PageList<Userlogin>(page.getTotal(), page.getRecords());
    }
}
